<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%;"
  >
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="delDepartment">
            <span> 操作<i class="el-icon-arrow-down"></i> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentsApi } from '@/api/departments'
export default {
  props: {
    // 定义传入的属性
    nodeData: {
      // 全部数据
      type: Object,
      required: true
    },
    // 编辑部门和删除部门隐藏还是不隐藏
    isRoot: {
      type: Boolean,
      default: false // 默认值：不隐藏
    }
  },
  methods: {
    // 通过形参command接收的command属性传递过来的值判断你点的是哪个
    delDepartment(command) {
      if (command === 'add') {
        // 这里是点击了添加
        this.$emit('openDialog', this.nodeData) // 这里的nodedata传给了父组件data接收
      }
      if (command === 'edit') {
        // 这里是点击了编辑
        this.$emit('editDepts', this.nodeData)
      }
      if (command === 'del') {
        // 这里是点击了删除
        this.$confirm('此操作将永久删除该部门, 是否继续?', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            await delDepartmentsApi(this.nodeData.id)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.$emit('delDeptsFn')
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      }
    }
  }
}
</script>

<style></style>
